<template>
    <div>
        <el-dialog
                :title="isEdit ? '编辑' : '新增'"
                :visible.sync="dialogVisible"
                width="50%"
                :before-close="handleClose">
            <el-form v-if="isEdit" label-position="left" label-width="80px" :model="userData">
                <el-form-item label="教师姓名" prop="teachername">
                    <el-input v-model="userData.teachername"/>
                </el-form-item>
                <el-form-item label="教师邮箱">
                    <el-input v-model="userData.email"/>
                </el-form-item>

                <el-form-item label="教师性别" prop="departmentname">
                    <el-radio-group v-model="userData.gender">
                        <el-radio label='男'>男</el-radio>
                        <el-radio label=‘女’>女</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="学院名称" prop="departmentname">
                    <el-select v-model="userData.departmentid" placeholder="学院名称">
                        <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.departmentname"
                                :value="item.departmentid">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>

            <el-form v-else label-position="left" label-width="80px" :model="addTeacherForm">
                <el-form-item label="教师姓名">
                    <el-input v-model="addTeacherForm.teachername"/>
                </el-form-item>
                <el-form-item label="教师邮箱">
                    <el-input v-model="addTeacherForm.email"/>
                </el-form-item>
                <el-form-item label="教师性别">
                    <el-radio-group v-model="addTeacherForm.gender">
                        <el-radio label='男'>男</el-radio>
                        <el-radio label=‘女’>女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="学院名称">
                    <el-select v-model="addTeacherForm.departmentid" placeholder="学院名称">
                        <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.departmentname"
                                :value="item.departmentid">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
				<el-button @click="handleClose">取 消</el-button>
    		<el-button type="primary" @click="onSubmit" v-loading="loading">确 定</el-button>
  		</span>
        </el-dialog>
    </div>
</template>

<script>
    import * as college from '@/api/college';
    import * as teacher from '@/api/teacher';

    export default {
        name: 'addDialog',
        props: {
            dialogVisible: {
                type: Boolean,
                default: false
            },
            isEdit: {
                type: Boolean
            },
            userData: {
                type: Object,
                default: {}
            },
            departmentname: ''
        },
        data() {
            return {
                loading: false,
                teacherForm: {
                    teacherid: '',
                    teachername: '',
                    departmentid: '',
                    gender: '',
                    email: ''
                },
                addTeacherForm: {
                    teachername: '',
                    departmentid: '',
                    gender: '',
                    email: ''
                },
                options: []
            }
        },
        methods: {
            onSubmit() {
                if (this.isEdit) {
                    this.onEditTeacher();
                } else {
                    this.onAddTeacher();
                }
            },
            async onAddTeacher() {
                try {
                    this.loading = true;
                    await teacher.addTeacher(this.addTeacherForm);
                    this.$notify({
                        title: '信息',
                        type: 'success',
                        message: '添加成功!'
                    });
                    this.handleFresh();
                    this.handleClose();
                } catch (e) {
                    this.$notify({
                        title: '错误',
                        type: 'error',
                        message: '添加失败!'
                    });
                    console.log(e)
                } finally {
                    this.loading = false;
                }
            },
            async onEditTeacher() {
                try {
                    this.loading = true;
                    await teacher.editTeacher(this.userData);
                    this.$notify({
                        title: '信息',
                        type: 'success',
                        message: '修改成功!'
                    });
                    this.handleFresh();
                    this.handleClose();
                } catch (e) {
                    console.log(e);
                    this.$notify({
                        title: '错误',
                        type: 'error',
                        message: '修改失败!'
                    });
                } finally {
                    this.loading = false
                }
            },


            async getCollege() {
                const res = await college.fetchUserList()
                this.options = res.msg
            },
            handleClose() {
                this.$emit('closeDialog');
            },
            handleFresh() {
                this.$emit('fresh');
            }
        },
        watch: {
            dialogVisible(newVal, oldVal) {
                if (newVal) {
                    Object.assign(this.resumeForm, this.resumeData);
                    console.log(this.userData);
                    if (!this.isEdit) {
                        this.$nextTick(() => {
                            this.$refs['resumeForm'].resetFields();
                        })
                    }
                }
            }
        },

        created() {
            this.addTeacherForm = {
                teachername: '',
                departmentid: '',
                gender: '',
                email: ''
            };
            this.getCollege()
        }

    }
</script>

<style scoped>

</style>